// pages/home/weekly/createAlbum/createAlbum.js
import Dialog from '../../../../lib/vant/dialog/dialog';
const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');

//获取应用实例
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    albumTypeId: '', //专辑类型id
    albumTypeName: '', //专辑类型名称
    albumTitle: '',//词条名称
    albumSlogan: '',//词条宣传口号
    albumInfoList: [],// 专辑数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    // 获取专辑信息
    _this.getAlbumInfoByNotYetJoin();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 选择专辑类型
   */
  toSelectAlbumType: function(){
    wx.navigateTo({
      url: '../createAlbum/selectAlbumType/selectAlbumType',
    })
  },

  /**
   * 下一步
   */
  next: function(){
    var _this = this;
    if (_this.validation()) {
      var albumTitle = _this.data.albumTitle;
      var albumSlogan = _this.data.albumSlogan;
      var albumTypeId = _this.data.albumTypeId;
      var albumTypeName = _this.data.albumTypeName;
      wx.navigateTo({
        url: '../editorAlbumDetails/editorAlbumDetails?albumTitle=' + albumTitle + '&albumSlogan=' + albumSlogan + '&albumTypeId=' + albumTypeId + '&albumTypeName=' + albumTypeName + '&type=0'
      })
    }
  },

  /**
   * 词条名称失去焦点事件
   */
  albumTitle: function (e) {
    var _this = this;
    var albumTitle = e.detail.value;
    _this.setData({
      albumTitle: albumTitle
    })
  },

  /**
   * 词条口号失去焦点事件
   */
  albumSlogan: function (e) {
    var _this = this;
    var albumSlogan = e.detail.value;
    _this.setData({
      albumSlogan: albumSlogan
    })
  },

  /**
   * 校验
   */
  validation: function(){
    var _this = this;
    var albumTypeId = _this.data.albumTypeId;
    var albumTitle = _this.data.albumTitle;
    var albumSlogan = _this.data.albumSlogan;
    console.log(albumTypeId)
    if (albumTitle.length <= 0) {
      _this.DialogAlert("请输入词条名称");
      return false;
    }
    if (albumTypeId.length <= 0) {
      _this.DialogAlert("请选择专辑类型");
      return false;
    }
    if (albumSlogan.length <= 0) {
      _this.DialogAlert("请输入宣传口号");
      return false;
    }
    return true;
  },

  /**
   * Dialog弹窗
   */
  DialogAlert: function (e) {
    Dialog.alert({
      message: e,
      showConfirmButton: false,
      overlay: false
    });

    setTimeout(() => {
      Dialog.close();
    }, 1000);
  },

  /**
   * 获取尚未加入的兴趣专辑
   */
  getAlbumInfoByNotYetJoin: function(){
    var _this = this;
    var url = "";
    var data = {};
    data.pageNum = 1;
    data.pageSize = 3;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        url = api.Home_GetAlbumInfoByNotYetJoinApiUrl;
        _this.getAlbumInfoByNotYetJoinQuery(url, data);
      })
    }
  },

  /**
   * 获取尚未加入的兴趣专辑：请求
   */
  getAlbumInfoByNotYetJoinQuery: function(url, data){
    var _this = this;
    util.post(url, data).then(function (res) {
      if (res.code == 200) {
        var list = res.result.list;
        _this.setData({
          albumInfoList: list
        })
      }
    })
  }
})